package com.gameplatform.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gameplatform.model.Rating;
import com.gameplatform.service.RatingService;
import com.gameplatform.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RatingServlet extends HttpServlet {
    private RatingService ratingService = new RatingService();
    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Rating rating = objectMapper.readValue(request.getInputStream(), Rating.class);
        boolean success = ratingService.createRating(rating);

        if (success) {
            ResponseUtil.writeResponse(response, 201, "Rating created successfully", rating);
        } else {
            ResponseUtil.writeResponse(response, 500, "Failed to create rating", null);
        }
    }
}